package org.walkerljl.boss.service.monitor.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.walkerljl.boss.dao.identity.AppDao;
import org.walkerljl.boss.dao.monitor.MonitorObjectDao;
import org.walkerljl.boss.dao.monitor.MonitorResultDao;
import org.walkerljl.boss.domain.identity.App;
import org.walkerljl.boss.domain.monitor.MonitorObject;
import org.walkerljl.boss.domain.monitor.MonitorResult;
import org.walkerljl.boss.service.monitor.MonitorResultService;
import org.walkerljl.boss.support.dao.BaseDao;
import org.walkerljl.boss.support.service.impl.JqueryDatatableBaseServiceImpl;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * MonitorResultServiceImpl
 *
 * @author lijunlin
 */
@Service("monitorResultService")
public class MonitorResultServiceImpl extends JqueryDatatableBaseServiceImpl<Long, MonitorResult> implements MonitorResultService {

    @Resource
    private MonitorResultDao monitorResultDao;
    @Resource
    private AppDao appDao;
    @Resource
    private MonitorObjectDao monitorObjectDao;

    @Override
    public BaseDao<Long, MonitorResult> getDao() {
        return monitorResultDao;
    }

    @Override
    public Map<String, Object> selectJqueryDatatablePage(MonitorResult condition) {
        Map<String, Object> resultMap = super.selectJqueryDatatablePage(condition);
        List<MonitorResult> monitorResults = (List<MonitorResult>) resultMap.get(DATATABLE_DATA_KEY);
        if (CollectionUtils.isEmpty(monitorResults)) {
            return resultMap;
        }
        for (MonitorResult ele : monitorResults) {
            App app = appDao.selectByKey(ele.getAppId());
            if (app != null) {
                ele.setAppName(app.getName());
            }

            MonitorObject monitorObject = monitorObjectDao.selectByKey(ele.getMonitorObjectId());
            if (monitorObject != null) {
                ele.setMonitorObjectName(monitorObject.getTarget());
            }
        }
        return resultMap;
    }
}
